package com.xxl.job.admin.interceptor;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import com.xxl.job.admin.config.SwaggerApp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * web mvc config
 *
 * @author xuxueli 2018-04-02 20:48:20
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private PermissionInterceptor permissionInterceptor;
    @Resource
    private CookieInterceptor cookieInterceptor;

    private final SwaggerApp swaggerApp;

    @Autowired
    public WebMvcConfig(SwaggerApp swaggerApp) {
        this.swaggerApp = swaggerApp;
        log.debug("拦截器全局配置器启动！");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.debug("开始配置拦截器！");
        log.debug("配置了一个拦截器：RequestInterceptor");
        registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**");
        log.debug("配置了一个拦截器：SeataHandlerInterceptor");
        registry.addInterceptor(new SeataHandlerInterceptor()).addPathPatterns("/**");
        log.debug("配置了一个拦截器：LogInterceptor");
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
        log.debug("配置了一个拦截器，来源于xxl-job：permissionInterceptor");
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**");
        log.debug("配置了一个拦截器，来源于xxl-job：cookieInterceptor");
        registry.addInterceptor(cookieInterceptor).addPathPatterns("/**");
        log.debug("配置拦截器结束！");
    }

    /**
     * 映射资源文件
     *
     * @param registry ResourceHandlerRegistry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.debug("开始映射资源文件！");
        log.debug("映射Swagger资源文件！");
        swaggerApp.addResourceHandlers(registry);
        log.debug("映射资源文件结束！");
    }
}
